package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConst;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public Result addOrder(Map<String, Object> map) throws Exception {

        //获取选择的日期
        String orderDate =String.valueOf(map.get("orderDate"));
        //获取电话号
        String telephone = String.valueOf(map.get("telephone"));
        //获取姓名
        String name = String.valueOf(map.get("name"));
        //获取性别
        String sex = String.valueOf(map.get("sex"));
        //获取身份证号
        String idCard = String.valueOf(map.get("idCard"));
        //获取套餐id
        Integer setmealId = Integer.parseInt(String.valueOf(map.get("setmealId")));
        //获取预约类型
        String orderType = String.valueOf(map.get("orderType"));
        //获取预约设置对象
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByDate(date);
        //判断该日期是否进行了设置
        if (orderSetting == null){
            return new Result(false, MessageConst.GET_ORDERSETTING_FAIL);
        }
        //该日期是否预约已满
        if (orderSetting.getReservations()>orderSetting.getNumber()){
            return new Result(false,MessageConst.ORDER_FULL);
        }
        //判断过期身份是不是会员
        Member member = memberDao.findBytelephone(telephone);
        if (member==null){
            member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());

            memberDao.add(member);
        }else {
            //已经是会员
            Order order = new Order();
            order.setOrderDate(date);
            order.setOrderType(orderType);
            order.setMemberId(member.getId());
            order.setSetmealId(setmealId);
            //判断是否重复预约
            long count = orderDao.getOrder(order);
            if (count>0){
                return new Result(false,MessageConst.HAS_ORDERED);
            }
        }
        //添加预约信息
        Order order = new Order();
        order.setOrderDate(date);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO_TIME);
        order.setSetmealId(setmealId);
        order.setMemberId(member.getId());
        orderDao.addOrder(order);

        //成功后添加预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        //更新预约设置
        orderSettingDao.edit(orderSetting);
        return new Result(true,MessageConst.ORDER_SUCCESS,order);
    }

    @Override
    public Map<String, Object> findById(Integer id) {

        return orderDao.findById(id);
    }
}
